package com.hrb.monkey.frament;


import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.text.HtmlCompat;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.hrb.commonsdk.okhttp.listener.DisposeDataListener;
import com.hrb.monkey.R;
import com.hrb.monkey.network.http.RequestCenter;
import com.hrb.monkey.record.OneArticleRecord;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class OneArticleFragment extends Fragment {


    @BindView(R.id.article_title)
    TextView articleTitle;
    @BindView(R.id.article_author)
    TextView articleAuthor;
    @BindView(R.id.article_wc)
    TextView articleWc;
    @BindView(R.id.article_content)
    TextView articleContent;
    Unbinder unbinder;

    public OneArticleFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one_article, container, false);
        unbinder = ButterKnife.bind(this, view);
        //text文本框出现滚动条
        articleContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        articleContent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });
        initData();
        return view;
    }

    private void initData() {
        RequestCenter.getRequest("https://interface.meiriyiwen.com/article/random?dev=1", null, new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                OneArticleRecord record = (OneArticleRecord) responseObj;
                articleAuthor.setText(record.getData().getAuthor());
                articleContent.setText(Html.fromHtml(record.getData().getContent()));
                articleTitle.setText(record.getData().getTitle());
                articleWc.setText(record.getData().getWc() + "");
            }

            @Override
            public void onFailure(Object reasonObj) {

            }
        }, OneArticleRecord.class);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
        /**
         * 发生确定的单击时执行
         * @param e
         * @return
         */
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {//单击事件

//            Toast.makeText(getContext(), "这是单击事件", Toast.LENGTH_SHORT).show();
            return super.onSingleTapConfirmed(e);
        }

        /**
         * 双击发生时的通知
         * @param e
         * @return
         */
        @Override
        public boolean onDoubleTap(MotionEvent e) {//双击事件
            initData();
//            Toast.makeText(getContext(), "双击事件", Toast.LENGTH_SHORT).show();
            return super.onDoubleTap(e);
        }

        /**
         * 双击手势过程中发生的事件，包括按下、移动和抬起事件
         * @param e
         * @return
         */
        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return super.onDoubleTapEvent(e);
        }


    });

}
